package com.magic.gre.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;

import com.noname.lib_base_java.entity.MsgEvent;
import com.noname.lib_base_java.net.RxBus;
import com.noname.lib_base_java.net.download.DownloadInfo;
import com.noname.lib_base_java.net.download.DownloadManager;
import com.noname.lib_base_java.util.L;

import io.reactivex.Observer;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

/**
 * Author: Lmoumou
 * Date: 2019-02-27 14:41
 * 文件名: DownService
 * 描述:
 */
public class DownService extends Service {

    private static final String TAG = "DownService";

    private CompositeDisposable compositeDisposable = new CompositeDisposable();

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        L.e(TAG, "onCreate");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        L.e(TAG, "onStartCommand");
        DownloadInfo info = intent.getParcelableExtra("info");
        if (info == null) {
            stopSelf();
        } else {
            DownloadManager downloadManager = DownloadManager.getInstance();
            downloadManager.setProgressListener(new DownloadManager.ProgressListener() {
                @Override
                public void progressChanged(long read, long contentLength, boolean done) {
                    int pro = (int) (((double) read / (double) contentLength) * 100);
                    MsgEvent msgEvent = new MsgEvent(MsgEvent.DOWANLOAD_PRO);
                    msgEvent.put("pro", pro);
                    RxBus.getInstance().post(msgEvent);
                    if (done) {
                        DownService.this.stopSelf();
                    }
                }

                @Override
                public void onAreadyDownload(DownloadInfo info) {
                    MsgEvent msgEvent=new MsgEvent(MsgEvent.DOWANLOAD_FINISH);
                    msgEvent.put("DownloadInfo",info);
                    RxBus.getInstance().post(msgEvent);
                    DownService.this.stopSelf();
                }
            });

            downloadManager.start(new Observer<DownloadInfo>() {
                @Override
                public void onSubscribe(Disposable d) {
                    compositeDisposable.add(d);
                }

                @Override
                public void onNext(DownloadInfo info) {
                    MsgEvent msgEvent=new MsgEvent(MsgEvent.DOWANLOAD_FINISH);
                    msgEvent.put("DownloadInfo",info);
                    RxBus.getInstance().post(msgEvent);
                    DownService.this.stopSelf();
                }

                @Override
                public void onError(Throwable e) {
                    DownService.this.stopSelf();
                }

                @Override
                public void onComplete() {

                }
            }, info);
        }

        return super.onStartCommand(intent, flags, startId);

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        L.e(TAG, "onDestroy");
        compositeDisposable.dispose();
    }
}
